package com.example.game.app.util;

import com.alibaba.fastjson.JSON;
import com.example.game.app.domain.WPParam;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class WPCodecUtil {

    //解码方法
    public static WPParam decode(String wp) {
        if (wp == null) {
            return new WPParam();
        }
        try {
            byte[] decodeBytes = Base64.getDecoder().decode(wp);
            String json = new String(decodeBytes, StandardCharsets.UTF_8);
            return JSON.parseObject(json, WPParam.class);
        }catch (Exception e) {
            throw new RuntimeException("WPParam 解码失败", e);
        }
    }

    //编码方法
    public static String encode(WPParam wpParam) {
        try {
            String json = JSON.toJSONString(wpParam);
            return Base64.getEncoder().encodeToString(json.getBytes(StandardCharsets.UTF_8));
        }catch (Exception e) {
            throw new RuntimeException("WPParam 编码失败");
        }
    }
}
